//
//  TextViewController.m
//  HeXun
//
//  Created by 6009 on 15/11/20.
//  Copyright © 2015年 LGC. All rights reserved.
//

#import "TextViewController.h"
#import "GetXMLObject.h"
#import "XMLModel.h"
#import "NEWSModel.h"
#import "UIImage+WebP.h"
#import "MBProgressHUD.h"
@interface TextViewController ()
{

    __weak IBOutlet UIView *_fontView;
    __weak IBOutlet UIView *_shareView;
    UIImage* _image;
    //记录收藏标记
    NSUInteger _bb;
}
@end

@implementation TextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textView.editable = NO;
    _bb = 1;
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSBlockOperation* cOperation = [NSBlockOperation blockOperationWithBlock:^{
        
        GetXMLObject* xmlObject = [GetXMLObject new];
        NEWSModel* newsModel = [xmlObject getSomeoneNewFromNewsID:self.xmlModel.ID];
        if (newsModel.content == nil)
        {
            newsModel.content = @"<p>nadata";
        }
        else
        {
            newsModel.content = [xmlObject deletelHTMLNode:newsModel.content];
        }
        NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc]initWithString:newsModel.content];
        if (newsModel.matchpic != nil)
        {
            _image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[xmlObject getStrArrFrom:newsModel.matchpic using:@","].firstObject]]];
            //图
            NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil] ;
            
            textAttachment.image = _image; //要添加的图片
            NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment] ;
            
            [attributeStr insertAttributedString:textAttachmentString atIndex:0];//index为用户指定要插入图片的位置
        }

        
        NSBlockOperation* dOperation = [NSBlockOperation blockOperationWithBlock:^{
             self.textView.attributedText = [[NSAttributedString alloc]initWithAttributedString:attributeStr];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
        //涉及更新UI的操作，就要创建包含这些操作的Operation对象，并把它放入主线程队列
        NSOperationQueue* mainQueue = [NSOperationQueue mainQueue];
        //当Operation放入主线程队列中，其操作就是子主线程中执行
        [mainQueue addOperation:dOperation];
    }];
    
    //创建Operation队列,operation只有在队列里才能跑
    NSOperationQueue* takeMoneyQueue = [NSOperationQueue new];
    
    [takeMoneyQueue addOperation:cOperation];
    
}

#pragma mark - item功能实现

- (IBAction)itemButtonPress:(UIBarButtonItem *)sender {
    
    if ( sender.tag == 0 ) {      //返回按钮
        [self.navigationController popViewControllerAnimated:YES];
        self.navigationController.navigationBarHidden = NO;
        
    }else if ( sender.tag == 1 ){ //进入评论
        
        
        
    }else if ( sender.tag == 2 ){ //显示字体窗口
        
        if ( _fontView.hidden ) {
            _fontView.hidden = NO;
            _shareView.hidden = YES;
        }else{
            _fontView.hidden = YES;
        }
        
    }else if ( sender.tag == 3 ){ //收藏
        
        if ( _bb == 1 ) {
            sender.image = [UIImage imageNamed:@"star.png"];
            _bb = 2;
        }else{
            sender.image = [UIImage imageNamed:@"collection.png"];
            _bb = 1;
        }
        
    }else if ( sender.tag == 4 ){ //显示分享窗口
        
        if ( _shareView.hidden ) {
            _shareView.hidden = NO;
            _fontView.hidden = YES;
        }else{
            _shareView.hidden = YES;
        }
        
    }
}

#pragma mark - 设置字体大小

- (IBAction)fontButtonPress:(UIButton *)sender {
    if ( sender.tag == 0 ) {
        self.textView.font = [UIFont italicSystemFontOfSize:12];
    }else if ( sender.tag == 1 ){
        self.textView.font = [UIFont italicSystemFontOfSize:22];
    }else if ( sender.tag == 2 ){
        self.textView.font = [UIFont italicSystemFontOfSize:32];
    }
}

#pragma mark - 分享到其他地方

- (IBAction)shareButtonPress:(UIButton *)sender {
    if ( sender.tag == 0 ) {
        NSLog(@"和讯微博");
    }else if ( sender.tag == 1 ){
        NSLog(@"新浪微博");
    }else if ( sender.tag == 2 ){
        NSLog(@"腾讯微博");
    }else if ( sender.tag == 3 ){
        NSLog(@"微信");
    }else if ( sender.tag == 4 ){
        NSLog(@"朋友圈");
    }else if ( sender.tag == 5 ){
        NSLog(@"跳转短信");
        
    }else if ( sender.tag == 6 ){
        NSLog(@"跳转邮箱");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzlzh.com"]];
    }
    
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
